<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>申请公章</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="公章名称:" prop="name">
            <el-select v-model="editForm.seal_id" placeholder="请选择证照">
              <el-option
                  v-for="item in arryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公章用途:">
            <el-input v-model="editForm.purpose" auto-complete="off" placeholder="填写借用用途"></el-input>
          </el-form-item>
          <el-form-item label="使用日期:">
            <el-date-picker type="date"
                            placeholder="选择日期"
                            v-model="editForm.time"
                            :picker-options="pickerOptions1"
                            value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <!--          <el-form-item label="用章文件:">-->
          <!--            <el-upload-->
          <!--                class="upload-demo"-->
          <!--                :action="imgSrcHttp"-->
          <!--                :on-remove="handleRemove"-->
          <!--                :on-success="handleSuccess"-->
          <!--                :before-upload="beforeAvatarUpload"-->
          <!--                :file-list="fileList"-->
          <!--                accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"-->
          <!--                multiple-->
          <!--                :limit="1">-->
          <!--              <el-button size="small" class="el-icon-upload">点击上传相应文件</el-button>-->
          <!--              <div slot="tip" class="el-upload__tip">上传word文件</div>-->
          <!--            </el-upload>-->
          <!--          </el-form-item>-->
          <el-form-item label="用章文件:">
            <!--            this.$refs.uploadFileModle.getInit() 清空-->
            <uploadFileModle number="20" @getFile="getFile" ref="uploadFileModle" :sycnStatus="true"
                             fileType="seal_add"></uploadFileModle>
          </el-form-item>
          <el-form-item label="补充说明:" prop="name">
            <el-input v-model="editForm.remark" auto-complete="off" placeholder="补充说明" type="textarea"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click="editSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ['visible', 'sealId'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      arryList: [],
      fileList: [],
      editForm: {
        purpose: '',//	整型	必填	1	-	用途
        seal_id: '',//	整型	必填	1	-	公章id
        time: '',//	整型	必填	1	-	使用时间
        file: '',//	整型	必填	1	-	用章文件
        remark: '',//	整型	必填	1	-	补充说明
      }, // 编辑界面数据
      // 限制时间选择
      pickerOptions1:
          {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7
            }
          },
      imgSrcHttp: this.api.common.office
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.sealId) {
          this.getDetail();
        }
        this.getLicenceList()
      } else {
        for (let i in this.editForm) {
          this.editForm[i] = '';
        }
      }
    }
  },
  created() {
  },
  methods: {
    getLicenceList() {
      this.$fetch(this.api.seal.index, {p: 1}).then((response) => {
        if (response.code === 0) {
          this.arryList = this.getJson(response.list.data)
          if (this.arryList.length === 1) {
            this.editForm.seal_id = this.arryList[0].id
          }
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getDetail() {
      this.$fetch(this.api.seal.apply_edit, {id: this.sealId}).then((response) => {
        if (!response.code) {
          this.editForm.purpose = response.vo.purpose;
          this.editForm.seal_id = response.vo.seal_id;
          this.editForm.time = response.vo.time;
          this.editForm.file = response.vo.file;
          this.editForm.remark = response.vo.remark;
          this.fileList = [
            {name: '用章文件', url: response.vo.file}
          ]
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if (!dataOption.seal_id) {
        this.$message.error('选择需要申请的公章')
        return
      }
      if (!dataOption.time) {
        this.$message.error('选择使用日期')
        return
      }
      // if (!dataOption.file) {
      //   this.$message.error('上传用章文件')
      //   return
      // }
      let url = this.api.seal.apply;
      let file = []
      for (let item of this.fileList) {
        let a = {}
        a.name = item.name
        a.src = item.response?item.response.data.pic:item.url
        file.push(a)
      }
      dataOption.file = JSON.stringify(file)
      console.log('dataOption:',dataOption)
      console.log('this.fileList:',this.fileList)

      let data = await this.$post(url, dataOption);
      if (!data.code) {
        this.$message.success('提交成功！')
        this.$emit('InitFn')
        this.closePOpup()
        this.clearnFn()
      } else {
        this.$message.error(data.msg)
      }
    },
    clearnFn() {
      this.editForm = {
        purpose: '',//	整型	必填	1	-	用途
        seal_id: '',//	整型	必填	1	-	公章id
        time: '',//	整型	必填	1	-	使用时间
        file: '',//	整型	必填	1	-	用章文件
        remark: '',//	整型	必填	1	-	补充说明
      } // 编辑界面数据
      this.$refs.uploadFileModle.getInit()
    },
    beforeAvatarUpload(file) {
      console.log('上传数据excel：', file)
      // const isLt2M = file.size / 1024 / 1024 < 2
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!')
      // }
      // return isLt2M
    },
    handleSuccess(response) {
      this.editForm.file = response.data.pic
    },
    handleRemove(file, fileList) {
      console.log('移除', file, fileList)
      this.editForm.file = ''
    },
    getFile(option) {
      console.log('shuju:', option)
      this.fileList = this.getJson(option)
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


